<?php
namespace app\equi\controller;

class User extends Base{

	//用户登录
	// http://192.168.200.99/equi/user/login
	// username
	// password
	// equi
	public function login(){

		

		$User = model("User");
		$res = array("code" => 0 ,"msg" => "登录成功","data" => "");
		if(request()->isPost()){
			$data = input('');
			//print_r($data);
			if(!isset($data) || !isset($data['password']) || !isset($data['username']) ){
				$res['code'] = 999;
				$res['msg'] = "请填写完整信息";
				die(myJSON($res));
			}else{
				//存在用户名密码检测是否正确
				$chRes = $User->loginCheck($data['username'],$data['password']);

				if( isset($chRes['code']) ){
					//检测失败返回信息
					$res = array_merge($res,$chRes);
					die(myJSON($res));
				}else{
					//检测成功，返回信息
					$res['data'] = nullToSpace($chRes);
					die(myJSON($res));
				}
			}
		}else{
				$res['code'] = 999;
				$res['msg'] = '非法请求！';
				die(myJSON($res));

		}
	}

	//请求 二维码地址
	//http://127.0.2.1/equi/user/getqrcode
	//equipment_id=1
	//{"code":999,"msg":"错误","data":""}
	//{"code":0,"msg":"ok","data":{"url":"http:\/\/127.0.2.1\/equi\/user\/qrcode\/text\/A1B470925C1523026558D","exptime":60,"interval":3}}
	public function getqrcode(){
		$res = array("code" => 0 ,"msg" => "ok","data" => "");
		$equipment_id = input('equipment_id/d');

		if( !$equipment_id ){
				$res['code'] = 991;
				$res['msg'] = 'err:equipment_id:' . $equipment_id;//$equipment_id 没有找到
				die(myJSON($res));

		}



		$exptime = 60;
		$interval = 3;

		$data['qrcode'] = rand(100000,999999);
		$data['qrexptime'] = time() + $exptime - 3 ;
		$data['qruserid']= 0 ;

		$qrtext = "A{$equipment_id}B{$data['qrcode']}C{$data['qrexptime']}D";
		$url =  url('qrcode','text='.$qrtext , '' ,true);

		//插入数据库
		$rs = model('equipment')->where('id', $equipment_id )->update($data);

		if(!$rs){
				$res['code'] = 999;
				$res['msg'] = "错误" ;//$equipment_id 没有找到
				$res['sql'] =  db()->getLastSql();
				die(myJSON($res));
		}

		$res['data'] = ['url' => $url , 'exptime'=>$exptime , 'interval'=>$interval];
		die(myJSON($res));
	}


	//生成二维码
	public function qrcode(){
		// header("content-type: image/png");
		$text = input('text/s');
		$text = $text?$text:'lrxin666';
		\think\Loader::import('phpqrcode.phpqrcode');
		\QRcode::png($text, false ,'L',20,3); 
		die();
	}


	//http://127.0.2.1/equi/user/qrcodelogin
	//equipment_id=1
	//{"code":999,"msg":"登录fail","data":""}
	//{"code":0,"msg":"ok","data":{"user_id":3,"username":"用户3","status":1,"loginname":"3","password":"eccbc87e4b5ce2fe28308fd9f2a7baf3","sex":1,"birthday":"1986-02-23","mobile":"mobile","workphone":"workphone","qq":"qq","email":"email","address":"address","idcard":"idcard","create_time":"1970-01-01 08:00:00","update_time":"2018-03-12 02:47:26","last_login_time":1522997465,"last_login_ip":"192.168.200.99","login":38}}
	public function qrcodelogin(){
		$res = array("code" => 0 ,"msg" => "ok","data" => "");

		$equipment_id = input('equipment_id/d');
		$rs = model('equipment')->where('id',$equipment_id)->find();

		if( !$rs ){
			$res['code'] = 998;
			$res['msg'] = "fail";//$equipment_id 没有找到
			die(myJSON($res));
		};

		if( !$rs['qruserid'] ){
			$res['code'] = 999;
			$res['msg'] = "登录fail";//$equipment_id 没有找到
			die(myJSON($res));
		};

		$user = model('user')->where('user_id' , $rs['qruserid'] )->find();
		if( !$user ){
			$res['code'] = 997;
			$res['msg'] = "用户不存在".$rs['qruserid'];//$equipment_id 没有找到
			die(myJSON($res));
		};

		if( $user['status'] != 1 ){
			$res['code'] = 997;
			$res['msg'] = "用户状态：".$user['status_text'].',无法登录。';//$equipment_id 没有找到
			die(myJSON($res));
		};

		//登录成功，设置 bc_equipment qrcode qrexptime qruserid  = 0 
		model('equipment')->where('id',$equipment_id)->update(['qrcode' => 0 , 'qrexptime' => 0 , 'qruserid' => 0 ]);

		$res['data'] = $user;
		die(myJSON($res));

		die();
	}



}